﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Web
{
    public partial class Default : System.Web.UI.Page
    {
        string img = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            preview.Src = img;
        }

        protected void btnupload_Click(object sender, EventArgs e)
        {
            string msg = "";
            string ww = "0";
            HttpPostedFile file = photoFile.PostedFile;
            if (!file.ContentType.Contains("image"))
            {
                msg = "照片格式不合法";
                return;
            }
            string ext = Path.GetExtension(file.FileName).ToLower();
            if (ext != ".jpg" && ext != ".gif" && ext != ".png" && ext != ".jpeg")
            {
                msg = "请您上传jpg、gif、png图片";
            }
            if (file.ContentLength > 5 * 1024 * 1024)
            {
                msg = "请您上传512字节内的图片";
            }
            string newName = Guid.NewGuid().ToString();
            string tempPath = "image/upload/";

            img = tempPath + newName + ext;
            string filePath = Server.MapPath(img);
            tempPath = Server.MapPath(tempPath);
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
            {
                float sw = 0;
                System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                originalImage.GetThumbnailImage(497, 367, callback, IntPtr.Zero).Save(filePath);
                ww = sw.ToString();
                msg = img;
            }
            tipMsg.InnerText = "上传成功";
            photo.Src = msg;
            preview.Src = msg;
            ppath.Value = msg;
        }

        public bool ThumbnailCallback()
        {
            return false;
        }
    }
}